package com.future.servlet;

import com.future.biz.TownsServices;
import com.future.dao.AreaDAO;
import com.future.model.bo.UserBO;
import com.future.model.po.ThirdFillBlankModel;
import com.future.model.po.UserModel;
import com.future.model.vo.TownsServicesVO;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 对乡镇（街道）和社区（行政村）调查页面进行增查改
 * 
 * @author petter Jan 11, 2014
 */
public class TownsServlet extends BaseServlet {

	private TownsServices townsServices = null;

	@Override
	public void doGet(HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		String flag = request.getParameter("flag");
		if (flag != null && !("").equals(flag)) {
			int iflag = Integer.valueOf(flag);
			switch (iflag) {
			case 1:
				showServices(request, response);
				break;
			case 2:
				updateServices(request, response);
				break;
			}
		}
	}

	@Override
	public void doPost(	HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * 显示乡镇（街道）和社区（行政村）调查页面
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws java.io.IOException
	 */
	public void showServices(	HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		townsServices = new TownsServices();
		UserModel userModel = new UserModel();
		UserBO userBo = (UserBO) request.getSession().getAttribute("userBo");
		userModel.setId(userBo.getUser().getId());

		Map<List<TownsServicesVO>, ThirdFillBlankModel> map = townsServices.showServices(userModel);
		Iterator<Entry<List<TownsServicesVO>, ThirdFillBlankModel>> iterator = map.entrySet().iterator();
		List<TownsServicesVO> tsvos = null;
		ThirdFillBlankModel blankModel = null;
		while (iterator.hasNext()) {
			Entry<List<TownsServicesVO>, ThirdFillBlankModel> entry = iterator.next();
			tsvos = entry.getKey();
			blankModel = entry.getValue();
		}
		request.setAttribute("tsvos", tsvos);
		request.setAttribute("blankModel", blankModel);
		if("/gx_survey/communityShowServices".equals(request.getRequestURI())){
			request.getRequestDispatcher("/third.jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("/index.jsp").forward(request, response);
		}
		return;
	}

	/**
	 * 修改、添加乡镇（街道）和社区（行政村）调查页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws java.io.IOException
	 */
	public void updateServices(	HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		townsServices = new TownsServices();
		UserModel userModel = new UserModel();
		UserBO userBo = (UserBO) request.getSession().getAttribute("userBo");
		userModel.setId(userBo.getUser().getId());
		
		ThirdFillBlankModel fillBlankModel = new ThirdFillBlankModel();
		fillBlankModel.setId(userModel.getId());
		fillBlankModel.setItem0101(request.getParameter("item0101")!=""?Integer.valueOf(request.getParameter("item0101")):null);
		fillBlankModel.setItem0201(request.getParameter("item0201")!=""?Integer.valueOf(request.getParameter("item0201")):null);
		fillBlankModel.setItem0202(request.getParameter("item0202")!=""?Integer.valueOf(request.getParameter("item0202")):null);
		fillBlankModel.setItem0301(request.getParameter("item0301")!=""?Float.valueOf(request.getParameter("item0301")):null);
		fillBlankModel.setItem0401(request.getParameter("item0401")!=""?Integer.valueOf(request.getParameter("item0401")):null);
		fillBlankModel.setUserid(userModel.getId());
		
		TownsServicesVO servicesVO = null;
		String answer = request.getParameter("answer");
		String servicesid = request.getParameter("servicesid");
		String[] answers = answer.split(",");
		String[] servicesids = servicesid.split(",");
		List<TownsServicesVO> servicesVOs = new  ArrayList<TownsServicesVO>();
		
		for (int i = 0; i < servicesids.length; i++) {
			servicesVO = new TownsServicesVO();
			servicesVO.setAnswer(answers[i]!=""?Integer.valueOf(answers[i]):-1);
			servicesVO.setServicesid(servicesids[i]!=""?Integer.valueOf(servicesids[i]):null);
			servicesVO.setUserid(userModel.getId());
			servicesVOs.add(servicesVO);
		}
		boolean flag = townsServices.updateServices(servicesVOs, fillBlankModel);
		AreaDAO areaDao = new AreaDAO();
		areaDao.updateAreaByCode(userBo.getUser().getUsername());
		response.setContentType("application/json; utf-8");
		response.setHeader("pragma","no-cache");
		response.setHeader("cache-control","no-cache");
		
		PrintWriter out = response.getWriter();
		out.println(flag);
		out.flush();
		out.close();
		return;
	}

}
